﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Runtime.Serialization;

using Unis.Framework.ORM.Attributes;
using Unis.Framework.RuleBase.Common;
using KingRoad.EPM.PersonnelLogEntity.Model;
using KingRoad.EPM.PersonnelLogDAL;

namespace KingRoad.EPM.PersonnelLogDAL.PlmConstructionlogcivilService
{
    /// <summary> 
    /// 文件生成时间 2017/1/12 11:26:48
    /// </summary> 
    public class PlmConstructionlogcivilService : BaseService<PlmConstructionlogcivilInfo>, IPlmConstructionlogcivilService
    {
        public DataTable GetInfoListByCondition(string sqlWhere = " 1=1 ")
        {
            var strSql = "SELECT * FROM Plm_ConstructionLogCivil WHERE " + sqlWhere;

            var dt = DatabaseExcuteBySql.QuerybySql(strSql);
            return dt;
        }

        public bool InsterConstructionlogInfo(Dictionary<string, string> formValues)
        {

            var tableColumnNameList = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PlmConstructionlogcivilInfo>
                ("PlmConstructionlogcivil", "SelectTableColumns");
            var columnsList = tableColumnNameList.Select(t => t.TableColumnsName).ToList();

            var sqlStrColumn = String.Format("INSERT INTO Plm_ConstructionLogCivil (");
            var sqlStrValues = "(";

            formValues.ToList().ForEach(t =>
            {
                if (!columnsList.Contains(t.Key))
                {
                    //如果数据库中没有字段，则更改表结构，添加字段，默认都添加nvarchar(2000)
                    var alterTableStr = String.Format("ALTER TABLE dbo.Plm_ConstructionLogCivil ADD  {0} NVARCHAR(2000) ", t.Key);
                    DatabaseExcuteBySql.ExecuteNonQuery("", alterTableStr);
                }

                sqlStrColumn += t.Key + ",";
                sqlStrValues += "'" + t.Value + "',";

            });

            if (sqlStrColumn.Contains(","))
                sqlStrColumn = sqlStrColumn.Substring(0, sqlStrColumn.Length - 1);
            if (sqlStrValues.Contains(","))
                sqlStrValues = sqlStrValues.Substring(0, sqlStrValues.Length - 1);

            sqlStrColumn += ") VALUES ";
            sqlStrValues += ")";
            var executeStr = sqlStrColumn + sqlStrValues;
            var ret = DatabaseExcuteBySql.ExecuteNonQuery("", executeStr);
            return ret > 0;
        }

        public bool UpdateConstructionlogInfo(Dictionary<string, string> formValues)
        {
            var tableColumnNameList = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PlmConstructionlogcivilInfo>
              ("PlmConstructionlogcivil", "SelectTableColumns");
            var columnsList = tableColumnNameList.Select(t => t.TableColumnsName).ToList();

            var sqlStrColumn = String.Format("UPDATE Plm_ConstructionLogCivil SET ");

            formValues.ToList().ForEach(t =>
            {
                if (!columnsList.Contains(t.Key))
                {
                    //如果数据库中没有字段，则更改表结构，添加字段，默认都添加nvarchar(2000)
                    var alterTableStr = String.Format("ALTER TABLE dbo.Plm_ConstructionLogCivil ADD  {0} NVARCHAR(2000) ", t.Key);
                    DatabaseExcuteBySql.ExecuteNonQuery("", alterTableStr);
                }
                if (t.Key != "Clc_ID")
                    sqlStrColumn += t.Key + "='" + t.Value + "',";
            });

            if (sqlStrColumn.Contains(","))
                sqlStrColumn = sqlStrColumn.Substring(0, sqlStrColumn.Length - 1);

            var executeStr = sqlStrColumn + " WHERE Clc_ID='" + formValues["Clc_ID"] + "';";
            var ret = DatabaseExcuteBySql.ExecuteNonQuery("", executeStr);
            return ret > 0;
        }

        /// <summary>
        /// 根据总监办标号，和施工日期分组查询信息
        /// </summary>
        /// <param name="strWhere">sql语句</param>
        /// <returns></returns>
        public List<PlmConstructionlogcivilInfo> GetInfoListGroupByDirectorSection(string strWhere = " 1=1 ")
        {
            var entity = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PlmConstructionlogcivilInfo>("PlmConstructionlogcivil", "GroupObjectByDirectorSection", strWhere);
            return entity;
        }

        /// <summary>
        /// 更新总监办评述
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateDirectorCommentInfo(List<PlmConstructionlogcivilInfo> list)
        {
            var ret = 0;
            list.ForEach(t =>
            {
                ret += DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity("PlmConstructionlogcivil", "UpdateDirectorCommentInfo", t);
            });
            return ret >= list.Count;
        }

        /// <summary>
        /// 更新驻地办办评述
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateStationCommentInfo(List<PlmConstructionlogcivilInfo> list)
        {
            var ret = 0;
            list.ForEach(t =>
            {
                ret += DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity("PlmConstructionlogcivil", "UpdateStationCommentInfo", t);
            });
            return ret >= list.Count;
        }
    }

}
